package com.wanghua.mall.order.service;

import com.alipay.easysdk.factory.Factory;
import com.alipay.easysdk.kernel.Config;
import com.alipay.easysdk.payment.app.models.AlipayTradeAppPayResponse;
import com.alipay.easysdk.payment.common.models.*;
import com.alipay.easysdk.payment.page.models.AlipayTradePagePayResponse;
import com.alipay.easysdk.payment.wap.models.AlipayTradeWapPayResponse;
import com.wanghua.mall.order.constant.AlipayConstant;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;


/**
 * @author wanghua
 * @create 2021-09-10 20:26
 */
@Slf4j
@Service
public class AlipayService {


    public AlipayService() {
        //设置Factory 全局参数
        Config config = new Config();
        config.appId = AlipayConstant.APP_ID;
        config.alipayPublicKey = AlipayConstant.ALIPAY_PUBLIC_KEY;
        config.gatewayHost = "openapi.alipaydev.com";
        config.protocol = "https";
        config.signType = "RSA2";
        config.merchantPrivateKey = AlipayConstant.MERCHANT_PRIVATE_KEY;
        config.notifyUrl = AlipayConstant.NOTIFY_URL;
        //可设置AES密钥，调用AES加解密相关接口时需要（可选）
        config.encryptKey = AlipayConstant.ENCRYPT_KEY;
        Factory.setOptions(config);
    }

    /**
     * 对应 alipay.trade.app.pay 接口
     * 构造交易数据供商户app到支付宝下单
     */
    public AlipayTradeAppPayResponse createAppTradeForm(String subject, String tradeNo, String totalAmount) throws Exception {

        return Factory.Payment.App()
                //单独设置超时时间 默认15分钟
                .optional("timeout_express", AlipayConstant.TIMEOUT_EXPRESS)
                .pay(subject, tradeNo, totalAmount);
    }

    /**
     * 对应alipay.trade.page.pay 接口
     * 构造交易数据供pc端到支付宝下单
     */
    public AlipayTradePagePayResponse createWebTradeForm(String subject, String tradeNo, String totalAmount, String returnUrl) throws Exception {
        return Factory.Payment.Page()
                //单独设置超时时间 默认15分钟
                .optional("timeout_express", AlipayConstant.TIMEOUT_EXPRESS)
                .pay(subject, tradeNo, totalAmount, returnUrl);
    }

    /**
     * alipay.trade.wap.pay
     * 构造交易数据供wap端到支付宝下单
     */
    public AlipayTradeWapPayResponse createWapTradeForm(String subject, String tradeNo, String totalAmount, String quitUrl, String returnUrl) throws Exception {
        return Factory.Payment.Wap()
                //单独设置超时时间 默认15分钟
                .optional("timeout_express", AlipayConstant.TIMEOUT_EXPRESS)
                .pay(subject, tradeNo, totalAmount, quitUrl, returnUrl);
    }

    /**
     * 对应alipay.trade.query(统一收单线下交易查询)
     */
    public AlipayTradeQueryResponse queryTrade(String tradeNo) throws Exception {
        return Factory.Payment.Common().query(tradeNo);
    }

    /**
     * alipay.trade.cancel
     */
    public AlipayTradeCancelResponse cancelTrade(String tradeNo) throws Exception {
        return Factory.Payment.Common().cancel(tradeNo);
    }

    /**
     * alipay.trade.close(统一收单交易关闭接口)
     */
    public AlipayTradeCloseResponse closeTrade(String tradeNo) throws Exception {
        return Factory.Payment.Common().close(tradeNo);
    }

    /**
     * alipay.trade.refund(统一收单交易退款接口)
     */
    public AlipayTradeRefundResponse refundTrade(String tradeNo, String refundAmount) throws Exception {
        return Factory.Payment.Common().refund(tradeNo, refundAmount);
    }

    /**
     * alipay.trade.fastpay.refund.query(统一收单交易退款查询)
     */
    public AlipayTradeFastpayRefundQueryResponse refundQuery(String tradeNo, String outRequestNo) throws Exception {
        return Factory.Payment.Common().queryRefund(tradeNo, outRequestNo);
    }
}
